import {VJson} from "../types";
import {deepTraverPlanObject} from "../utils/helper";
import _ from 'lodash'

/**
 * rows / cols 加工
 * rows: [
 *      {
 *          cols: [
 *              {
 *                  view: 'text'
 *              },
 *              {
 *                  view: 'text'
 *              }
 *          ]
 *      }
 * ]
 *
 * 转化为:
 *
 * items:[
 *      {
 *          view: 'row',
 *          items: [
 *              {
 *                  view: 'col',
 *                  items: [
 *                      {
 *                          view: 'text'
 *                      }
 *                  ]
 *              },
 *              {
 *                  view: 'col',
 *                  items: [
 *                      {
 *                          view: 'text'
 *                      }
 *                  ]
 *              }
 *          ]
 *      }
 * ]
 *
 */
export const rowSetting1 = {
    gutter: {xs: 0, sm: 0, md: 4},
}

export const colSetting = {
    "1": {
        weight1: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 24,
            xl: 24,
        },

    },
    "2": {
        weight1: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 12,
            xl: 12,
        },

        weight2: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 24,
            xl: 24,
        }
    },
    "3": {
        weight1: {
            xs: 24,
            sm: 12,
            md: 12,
            lg: 8,
            xl: 8,
        },

        weight2: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 16,
            xl: 16,
        },

        weight3: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 24,
            xl: 24,
        }
    },
    "4": {
        weight1: {
            xs: 24,
            sm: 12,
            md: 12,
            lg: 8,
            xl: 6,
        },

        weight2: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 16,
            xl: 12,
        },

        weight3: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 24,
            xl: 18,
        },

        weight4: {
            xs: 24,
            sm: 24,
            md: 24,
            lg: 24,
            xl: 24,
        }
    }

}

export function rowsColsInner(rows: any[]): any[] {
    const items: any[] = []

    _.forEach(rows, row => {
        if (!_.isArray(row.cols)) {
            items.push(row)
            return
        }
        items.push({
            view: 'row',
            ...rowSetting1,
            items: _.map(row.cols.filter((c: any) => !!c), col => {
                if (col.view === 'col') {
                    return col

                } else if (typeof col.span !== 'undefined') {
                    return {
                        view: 'col',
                        span: col.span,
                        items: [
                            col
                        ]
                    }
                }

                let weight = col.weight || 1
                let colsInRow = row.colsInRow || 4
                if (colsInRow > 4) {
                    colsInRow = 4;
                }
                if (colsInRow < 1) {
                    colsInRow = 1;
                }
                if (weight>colsInRow) {
                    weight = colsInRow;
                }
                if (weight<1) {
                    weight = 1;
                }
                delete col.weight

                return {
                    view: 'col',
                    // @ts-ignore
                    ...colSetting[colsInRow + '']['weight' + weight],
                    items: [
                        col
                    ]
                }
            })
        })
    })

    return items
}

export function rowsCols(this: any, vjson: VJson): Promise<VJson> {
    return new Promise((resolve, reject) => {
        const scope = this
        let hasFound = true
        while (hasFound) {
            hasFound = false
            deepTraverPlanObject(vjson, (path, key, value) => {
                if (key === 'rows' && _.isArray(value)) {
                    // rows 下所有的 cols 才转换.
                    const items = rowsColsInner(value)

                    // 无条件 删除rows:[] 并替换同级别的 items:[]
                    const itemsParentPath = path.substring(0, path.lastIndexOf('.'))
                    const itemParent = (itemsParentPath ? _.get(vjson, itemsParentPath) : vjson)
                    delete itemParent['rows']
                    itemParent['items'] = items

                    hasFound = true
                    return true
                }
            })
        }
        resolve(vjson)
    })
}